package com.spogher.bsc.pages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.DataGridView;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.data.dao.interfaces.ChampionshipDao;
import com.spogher.bsc.data.dao.interfaces.PlayerDao;
import com.spogher.bsc.data.dao.interfaces.PlayerSheetDao;
import com.spogher.bsc.data.dao.interfaces.ScoreSheetDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Championship;
import com.spogher.bsc.data.dataobjects.PlayerSheet;
import com.spogher.bsc.data.dataobjects.ScoreSheet;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.bsc.models.ScoreSheetModel;
import com.spogher.components.dropdownChoices.ChampionshipDropDownChoice;
import com.spogher.components.dropdownChoices.PlayerDropDownChoice;
import com.spogher.components.dropdownChoices.TeamDropDownChoice;
import com.spogher.components.textfields.DatePickerTextField;

public class ScoreSheetPage extends BscTemplatePage{
	@SpringBean
	TeamDao teamDao;
	@SpringBean
	ChampionshipDao championshipDao;
	@SpringBean
	PlayerDao playerDao;
	@SpringBean 
	ScoreSheetDao scoreSheetDao;
	@SpringBean
	PlayerSheetDao playerSheetDao;

	public ScoreSheetPage (final PageParameters pp) {	
		super();
		final Form<ScoreSheet> scoreSheetForm = new Form<ScoreSheet>("scoreSheetForm", new ScoreSheetModel(new ScoreSheet()));
		scoreSheetForm.setOutputMarkupId(true);
		
		final WebMarkupContainer scoreSheetContainer = new WebMarkupContainer("scoreSheetContainer");
		final WebMarkupContainer boxScoreContainer = new WebMarkupContainer("boxScoreContainer");
		//create scoreSheet components
		scoreSheetContainer.add(new DatePickerTextField("date").setRequired(true));
		DropDownChoice<Championship> championshipChoice = new ChampionshipDropDownChoice("championship", championshipDao.findAll());
		scoreSheetContainer.add(championshipChoice);
		DropDownChoice<Team> homeTeamChoice = new TeamDropDownChoice("homeTeam", teamDao.findAll());
		scoreSheetContainer.add(homeTeamChoice);
		DropDownChoice<Team> awayTeamChoice = new TeamDropDownChoice("awayTeam", teamDao.findAll());	
		scoreSheetContainer.add(awayTeamChoice);
		scoreSheetContainer.add(new TextField<Integer>("homeTeamScore").setRequired(true));
		scoreSheetContainer.add(new TextField<Integer>("awayTeamScore").setRequired(true));
		scoreSheetContainer.add(new AjaxSubmitLink("submitScoreSheet") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				ScoreSheetModel formModel = (ScoreSheetModel) form.getModel();
				ScoreSheet scoreSheet = formModel.getObject();
				formModel.setObject(scoreSheetDao.save(scoreSheet));
				scoreSheetContainer.setEnabled(false);
				createBoxScoreComponents(scoreSheetForm, boxScoreContainer);
				boxScoreContainer.setVisible(true);
				target.addComponent(form);				
			}
		});		
		scoreSheetForm.add(scoreSheetContainer);
		
		//create box score components
		createBoxScoreComponents(scoreSheetForm, boxScoreContainer);
	}

	/**
	 * create box score components
	 * @param scoreSheetForm input data
	 * @param boxScoreContainer output data
	 */
	private void createBoxScoreComponents(Form<ScoreSheet> scoreSheetForm,
			final WebMarkupContainer boxScoreContainer) {
		
		List<ICellPopulator<PlayerSheet>> playerSheetColumns = new ArrayList<ICellPopulator<PlayerSheet>>();		
		IDataProvider<PlayerSheet> homePlayerSheetProvider = new PlayerSheetprovider(((ScoreSheetModel)scoreSheetForm.getModel()).getHomePlayerSheets());
		boxScoreContainer.addOrReplace(new PlayerSheetDataGridView("homePlayer", playerSheetColumns, homePlayerSheetProvider, scoreSheetForm.getModelObject().getHomeTeam()));
		
		IDataProvider<PlayerSheet> awayPlayerSheetProvider = new PlayerSheetprovider(((ScoreSheetModel)scoreSheetForm.getModel()).getAwayPlayerSheets());
		boxScoreContainer.addOrReplace(new PlayerSheetDataGridView("awayPlayer", playerSheetColumns, awayPlayerSheetProvider, scoreSheetForm.getModelObject().getAwayTeam()));
		
		
		boxScoreContainer.addOrReplace(new AjaxSubmitLink("submitBoxScore") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				ScoreSheetModel formModel = (ScoreSheetModel) form.getModel();
				ScoreSheet scoreSheet = formModel.getObject();
				for (PlayerSheet playerSheet: formModel.getHomePlayerSheets()) {
					playerSheet.setScoreSheet(scoreSheet);
					if (playerSheet.isValid()) {
						playerSheetDao.save(playerSheet);
					}
				}
				for (PlayerSheet playerSheet: formModel.getAwayPlayerSheets()) {
					playerSheet.setScoreSheet(scoreSheet);
					if (playerSheet.isValid()) {
						playerSheetDao.save(playerSheet);
					}
				}				
				form.setEnabled(false);
				target.addComponent(form);				
			}
		});
		boxScoreContainer.setVisible(false);
		
		scoreSheetForm.addOrReplace(boxScoreContainer);
		
		addOrReplace(scoreSheetForm);
	}
	
	private class PlayerSheetprovider implements IDataProvider<PlayerSheet> {

		private static final long serialVersionUID = 1L;
		private List<PlayerSheet> playerSheets;
		
		public PlayerSheetprovider (List<PlayerSheet> sheets){
			this.playerSheets = sheets;
		} 
		public void detach() {
			
		}
		
		public int size() {
			return playerSheets.size();
		}
		
		public IModel<PlayerSheet> model(PlayerSheet object) {
			return new CompoundPropertyModel<PlayerSheet>(object);
		}
		
		public Iterator<? extends PlayerSheet> iterator(int first, int count) {
			return playerSheets.iterator();
		}
		
	}
	
	private class PlayerSheetDataGridView extends DataGridView<PlayerSheet> {

		
		private static final long serialVersionUID = 1L;
		private Team team;

		public PlayerSheetDataGridView(String id,
				List<ICellPopulator<PlayerSheet>> populators,
				IDataProvider<PlayerSheet> dataProvider, Team team) {
			super(id, populators, dataProvider);
			this.team = team;
		}

		@Override
		protected Item<PlayerSheet> newRowItem(String id, int index,
				IModel<PlayerSheet> model) {
			Item<PlayerSheet> item = super.newRowItem(id, index, model);
			item.add(new TextField<String>("number"));
			item.add(new PlayerDropDownChoice("player", playerDao.findByTeam(team)));
			item.add(new TextField<String>("points"));
			item.add(new TextField<String>("minutes"));
			item.add(new TextField<String>("rebounds"));
			item.add(new TextField<String>("assists"));
			return item;
		}
		
		
		
	}
	
}
